package com.jml.service;

import com.jml.base.BaseService;
import com.jml.entity.ServiceInstance;
import com.jml.req.parameter.ReqInfoDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.TreeSet;

@Api(tags = "获取服务相关信息")
@RestController
public class RegisterService extends BaseService {

    private static final Log log = LogFactory.getLog(RegisterService.class);


    @Autowired
    private JmlRegisterContainer jmlRegisterContainer;

    /**
     * 提供注册服务接口
     * 127.0.0.1:8848/register
     * json:
     * {
     * "serviceAddres":"127.0.0.1:8080",
     * "serviceName":"jjj"
     * }
     *
     * @return
     */
    @ApiOperation("服务注册")
    @PostMapping("/register")
    public ResponseEntity register(@RequestBody ReqInfoDTO reqInfo) {
        boolean result = jmlRegisterContainer.put(reqInfo);
        return result ? setResultOk() : setResultFail();
    }

    /**
     * 查询活跃的服务列表
     * http://127.0.0.1:8848/activeService
     *
     * @return
     */
    @ApiOperation("获取所有服务")
    @GetMapping("/activeService")
    public Map<String, TreeSet<ServiceInstance>> activeService() {
        return jmlRegisterContainer.getMapAddres();
    }


    /**
     * 移除接口
     *
     * @param reqInfo
     * @return
     */
    @ApiOperation("移除服务")
    @PostMapping("/remove/service")
    public boolean removeService(@RequestBody ReqInfoDTO reqInfo) {
        return findByServiceInstance(reqInfo, true) != null;
    }

    /**
     * @param reqInfo,根据地址，获取服务信息
     * @return
     */
    @ApiOperation("获取服务信息")
    @PostMapping("/get/service")
    public ServiceInstance findByServiceInstance(@RequestBody ReqInfoDTO reqInfo) {
        return findByServiceInstance(reqInfo, false);
    }

    /**
     * @param reqInfo
     * @param isDelete, 返回值就是地址信息，如果为true就是要删除这个地址信息并且返回这个地址信息
     *                  否则就仅仅是返回这个信息
     * @return
     */
    private ServiceInstance findByServiceInstance(ReqInfoDTO reqInfo, boolean isDelete) {
        String serviceAddres = reqInfo.getServiceAddres();
        if (StringUtils.isEmpty(serviceAddres)) {
            return null;
        }
        TreeSet<ServiceInstance> serviceInstances = jmlRegisterContainer.getMapAddres().get(reqInfo.getServiceName());
        if (serviceInstances == null) {
            //没有找到服务列表
            log.error("<Service list not found>");
            return null;
        }
        for (ServiceInstance serviceInstance : serviceInstances) {
            if (serviceInstance.getServiceAddres().equals(serviceAddres)) {
                if (isDelete) {
                    serviceInstances.remove(serviceInstance);
                }
                return serviceInstance;
            }
        }
        return null;
    }

    
    @ApiOperation("服务续约接口")
    @PostMapping("/renewal")
    public boolean renewal(ReqInfoDTO reqInfo) {
        ServiceInstance serviceInstance = findByServiceInstance(reqInfo);
        if (serviceInstance == null) {
            // 则直接服务注册
            return jmlRegisterContainer.put(reqInfo);
        }
        // 修改续约时间
        serviceInstance.setLastRenewalTime(serviceInstance.getLastRenewalTime() + reqInfo.getRenewalDuration());
        return true;
    }
}
